package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduCourseDescription;
import com.atguigu.eduservice.entity.vo.CourseInfoForm;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.mapper.EduCourseMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseDescriptionService;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.handler.GuliExceptionHandler;
import com.atguigu.servicebase.handler.MyException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2022-02-10
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    //注入课程描述
    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;

    //注入课程章节和课程小节
    @Autowired
    private EduChapterService eduChapterService;

    @Autowired
    private EduVideoService eduVideoService;

    /**
     * courseInfo添加到课程表和课程简介表 并且是一对一的关系
    **/
    @Override
    public String addCourseInfoForm(CourseInfoForm courseInfo) {

        //1.添加到课程表
        //将courseInfo赋值到eduCourse
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfo,eduCourse);
        int insertFlag = baseMapper.insert(eduCourse);

        //添加失败
        if(insertFlag==0){
            throw new MyException(200001,"添加课程失败");
        }

        String courseId = eduCourse.getId();
        //2.添加到课程简介表
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        BeanUtils.copyProperties(courseInfo,eduCourseDescription);
        eduCourseDescription.setId(courseId);
        eduCourseDescriptionService.save(eduCourseDescription);

        return courseId;
    }

    /**
     * 根据课程id 获取课程表 和 课程描述表 中的信息
    **/
    @Override
    public CourseInfoForm getAllCourseInfoByCourseId(String courseId) {
        //1.根据课程id获取课程表中的课程
        EduCourse eduCourse = baseMapper.selectById(courseId);

        //创建CourseInfoForm
        CourseInfoForm courseInfoForm = new CourseInfoForm();
        //赋值
        BeanUtils.copyProperties(eduCourse,courseInfoForm);

        //2.根据课程id获取课程描述表中的课程信息
        EduCourseDescription description = eduCourseDescriptionService.getById(courseId);
        //赋值
        BeanUtils.copyProperties(description,courseInfoForm);

        return courseInfoForm;
    }

    /**
     * 修改 课程表 和 课程描述表 中的信息
    **/
    @Override
    public void updateCourseInfoForm(CourseInfoForm courseInfoForm) {
        //1.修改课程表
        //创建课程表类
        EduCourse eduCourse = new EduCourse();
        //赋值
        BeanUtils.copyProperties(courseInfoForm,eduCourse);
        //修改
        int flag = baseMapper.updateById(eduCourse);

        if(flag==0){
            throw new MyException(20001,"修改课程失败");
        }

        //2.修改课程描述表
        //创建课程描述表类
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        //赋值
//        BeanUtils.copyProperties(courseInfoForm,eduCourseDescription);错误
        eduCourseDescription.setId(courseInfoForm.getId());
        eduCourseDescription.setDescription(courseInfoForm.getDescription());
        //修改
        eduCourseDescriptionService.updateById(eduCourseDescription);
    }

    @Override
    public CoursePublishVo getCoursePublish(String id) {
        CoursePublishVo coursePublish = baseMapper.getCoursePublish(id);
        return coursePublish;
    }

    @Override
    public void deleteAllInfoByCourseId(String courseId) {
        //1.删除课程里面的章节
        eduChapterService.deleteCourseInfo(courseId);
        //2.删除课程里面的小节
        eduVideoService.deleteVideoByCourseInfo(courseId);
        //3.删除课程里面的描述
        eduCourseDescriptionService.removeById(courseId);
        //4.删除课程
        int resultFlag = baseMapper.deleteById(courseId);
        if(resultFlag == 0){
            throw new MyException(20001,"删除失败");
        }
    }
}
